/*
 * Copyright 2007 MBARI
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package org.mbari.vcr.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import org.mbari.vcr.IVCR;
import org.mbari.vcr.MonitoringVCR;

/**
 * Class description
 *
 *
 * @version    $Id: $
 * @author     Brian Schlining    
 */
public class VCRSelectionPanel extends JPanel {

    /**
     *
     */
    private static final long serialVersionUID = 3333456133704165945L;

    /**
     *     @uml.property  name="currentPanel"
     *     @uml.associationEnd  multiplicity="(1 1)"
     */
    private VCRConnectionPanel currentPanel = null;

    /**
     *     @uml.property  name="rs422RB"
     *     @uml.associationEnd
     */
    private JRadioButton rs422RB = null;

    /**
     *     @uml.property  name="rs422RBListener"
     */
    private ActionListener rs422RBListener = null;

    /**
     *     @uml.property  name="udp01RB"
     *     @uml.associationEnd
     */
    private JRadioButton udp01RB = null;

    /**
     *     @uml.property  name="udp01RBListener"
     */
    private ActionListener udp01RBListener = null;

    /**
     *     @uml.property  name="vcrConnectionPanelRs422"
     *     @uml.associationEnd
     */
    private VCRConnectionPanel vcrConnectionPanelRs422 = null;    //  @jve:decl-index=0:visual-constraint="520,305"

    /**
     *     @uml.property  name="vcrConnectionPanelUDP"
     *     @uml.associationEnd
     */
    private VCRConnectionPanel vcrConnectionPanelUDP = null;    //  @jve:decl-index=0:visual-constraint="405,351"

    /**
     *     @uml.property  name="vcrTypePanel"
     *     @uml.associationEnd
     */
    private JPanel vcrTypePanel = null;

    /**
     * This is the default constructor
     */
    public VCRSelectionPanel() {
        super();
        initialize();
    }

    /**
     *     This method initializes rs422RB
     *     @return  javax.swing.JRadioButton
     *     @uml.property  name="rs422RB"
     */
    private JRadioButton getRs422RB() {
        if (rs422RB == null) {
            rs422RB = new JRadioButton();
            rs422RB.setText("Direct (RS422)");
            rs422RB.addActionListener(getRs422RBListener());
            rs422RB.setSelected(true);

        }

        return rs422RB;
    }

    /**
     *     @return  the rs422RBListener
     *     @uml.property  name="rs422RBListener"
     */
    private ActionListener getRs422RBListener() {
        if (rs422RBListener == null) {
            rs422RBListener = new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    if (rs422RB.isSelected()) {
                        VCRSelectionPanel.this.remove(getVcrConnectionPanelUDP());
                        VCRSelectionPanel.this.add(getVcrConnectionPanelRs422(), BorderLayout.CENTER);
                        currentPanel = getVcrConnectionPanelRs422();
                        VCRSelectionPanel.this.revalidate();
                        VCRSelectionPanel.this.repaint();
                    }
                }
            };
        }

        return rs422RBListener;
    }

    private ActionListener getUdp01Listner() {
        if (udp01RBListener == null) {
            udp01RBListener = new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    if (udp01RB.isSelected()) {
                        VCRSelectionPanel.this.remove(getVcrConnectionPanelRs422());
                        VCRSelectionPanel.this.add(getVcrConnectionPanelUDP(), BorderLayout.CENTER);
                        currentPanel = getVcrConnectionPanelUDP();
                        VCRSelectionPanel.this.revalidate();
                        VCRSelectionPanel.this.repaint();
                    }
                }
            };
        }

        return udp01RBListener;
    }

    /**
     *     This method initializes udp01RB
     *     @return  javax.swing.JRadioButton
     *     @uml.property  name="udp01RB"
     */
    private JRadioButton getUdp01RB() {
        if (udp01RB == null) {
            udp01RB = new JRadioButton();
            udp01RB.setText("Ship Network (UDP)");
            udp01RB.addActionListener(getUdp01Listner());
        }

        return udp01RB;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public IVCR getVcr() {
        return currentPanel.getVcr();
    }

    /**
     *     This method initializes vcrConnectionPanelRs422
     *     @return  javax.swing.JPanel
     *     @uml.property  name="vcrConnectionPanelRs422"
     */
    private VCRConnectionPanel getVcrConnectionPanelRs422() {
        if (vcrConnectionPanelRs422 == null) {
            vcrConnectionPanelRs422 = new org.mbari.vcr.rs422.VCRConnectionPanel();
        }

        return vcrConnectionPanelRs422;
    }

    /**
     *     This method initializes vcrConnectionPanelUDP
     *     @return  javax.swing.JPanel
     *     @uml.property  name="vcrConnectionPanelUDP"
     */
    private VCRConnectionPanel getVcrConnectionPanelUDP() {
        if (vcrConnectionPanelUDP == null) {
            vcrConnectionPanelUDP = new org.mbari.vcr.udp01.VCRConnectionPanel();
        }

        return vcrConnectionPanelUDP;
    }

    /**
     *     This method initializes vcrTypePanel
     *     @return  javax.swing.JPanel
     *     @uml.property  name="vcrTypePanel"
     */
    private JPanel getVcrTypePanel() {
        if (vcrTypePanel == null) {
            vcrTypePanel = new JPanel();
            vcrTypePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Connection Type",
                    javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
                    javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
            vcrTypePanel.add(getRs422RB(), null);
            vcrTypePanel.add(getUdp01RB(), null);
        }

        return vcrTypePanel;
    }

    /**
     * This method initializes this
     *
     * @return void
     */
    private void initialize() {
        this.setLayout(new BorderLayout());
        this.add(getVcrTypePanel(), BorderLayout.NORTH);
        this.add(getVcrConnectionPanelRs422(), BorderLayout.CENTER);
        currentPanel = getVcrConnectionPanelRs422();
        ButtonGroup bg = new ButtonGroup();
        bg.add(getRs422RB());
        bg.add(getUdp01RB());
    }

    /**
     * Method description
     *
     *
     * @param vcr
     */
    public void setVcr(IVCR vcr) {
        if (vcr instanceof MonitoringVCR) {
            vcr = ((MonitoringVCR) vcr).getVcr();
        }

        if (vcr instanceof org.mbari.vcr.rs422.VCR) {
            getRs422RB().setSelected(true);
            getRs422RBListener().actionPerformed(null);
        }
        else if (vcr instanceof org.mbari.vcr.udp01.VCR) {
            org.mbari.vcr.udp01.VCR udpVcr = (org.mbari.vcr.udp01.VCR) vcr;
            getUdp01RB().setSelected(true);
            org.mbari.vcr.udp01.VCRConnectionPanel panel =
                (org.mbari.vcr.udp01.VCRConnectionPanel) getVcrConnectionPanelUDP();
            panel.getPortField().setText(udpVcr.getPort() + "");
            panel.getHostField().setText(udpVcr.getInetAddress().getHostName());
            getUdp01Listner().actionPerformed(null);
        }
    }
}
